package com.reviewit.mbean;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import com.reviewit.dao.DocLicenceDao;
import com.reviewit.model.DocLicence;

@ManagedBean
@SessionScoped
public class DocLicenceBean extends GenericBean implements Serializable,
		NavigationRules {
	private static final long serialVersionUID = 1L;

	private DataModel<DocLicence> docLicences;
	private DocLicence docLicence;
	private DocLicenceDao dao;

	public DocLicenceBean() {
		super();
		this.log = Logger.getLogger(this.getClass().getName());
		this.dao = DocLicenceDao.getInstance();
		this.docLicence = new DocLicence();
	}

	public DocLicence getDocLicence() {
		return docLicence;
	}

	public void setDocLicence(DocLicence docLicence) {
		this.docLicence = docLicence;
	}

	public void setDocLicences(DataModel<DocLicence> docLicences) {
		this.docLicences = docLicences;
	}

	public DataModel<DocLicence> getDocLicences() {
		List<DocLicence> list = dao.find().asList();
		docLicences = new ListDataModel<DocLicence>(list);
		return docLicences;
	}

	public void select() {
		setDocLicence(docLicences.getRowData());
	}

	public String findById() {
		DocLicence obj = dao.findOne("id", docLicence.getId());
		setDocLicence(obj);

		return NavigationRules.FIND_ENTITY;
	}

	public String saveOrUpdate() {
		DocLicence obj = dao.findOne("id", docLicence.getId());
		if (obj == null) {
			return this.update();
		} else {
			return this.save();
		}
	}

	public String save() {
		try {
			dao.save(docLicence);
		} catch (Exception e) {
			return e.getMessage();
		}

		setDocLicence(new DocLicence());
		return "DocLicence saved!";
	}

	public String update() {
		try {
			dao.save(docLicence);
		} catch (Exception e) {
			return e.getMessage();
		}

		setDocLicence(new DocLicence());
		return "DocLicence updated!";
	}

	public String delete() {
		try {
			dao.delete(docLicence);
		} catch (Exception e) {
			return e.getMessage();
		}

		setDocLicence(new DocLicence());
		return "DocLicence deleted!";
	}

	public String back() {
		return NavigationRules.BACK_CADASTRO;
	}

	public void nnew() {
		setDocLicence(new DocLicence());
	}
}